package com.qlm.service;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.qlm.cache.RankCache;
import com.qlm.entity.Rank;
import com.qlm.entity.User;
import com.qlm.tool.WxUtil;

public class GameMgr {
	
	static Logger logger = LoggerFactory.getLogger(GameMgr.class);

	public static void saveScore(User user,int score){
		if(score <= 0){
			return;
		}
		String openid = user.getStr(UserService.OPENID);
		String head_pic = user.getStr(UserService.HEAD_PIC);
		String nick_name = user.getStr(UserService.NICK_NAME);
		Rank rank = Rank.dao.findById(openid);
		if(rank == null){
			rank = new Rank();
			rank.set("openid", openid).set("score_", score).set("head_pic", head_pic).set("nick_name", nick_name).save();
		}else{
			Integer score_ = WxUtil.getInt("score_", rank);
			if(score > score_){
				rank.set("score_", score).update();
			}
		}
		RankCache.add(rank, score);
		logger.info("更新用户分数:"+openid+",分数:"+score);
	}
	
	public static List<Rank> getList(){
		return RankCache.getRanks();
	}
}
